package kr.co.yhcho.adoration.db;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

import android.app.Activity;
import android.content.Context;
import android.content.res.AssetManager;

public class InitDB extends Activity {
	private final static String ROOT_DIR = "/data/data/kr.co.yhcho.adorationproject/";
	private final static String DATABASE_NAME = "adoration.db";

	public static void initialize(Context ctx) {
		File folder = new File(ROOT_DIR + "databases");
		folder.mkdirs();
		File outfile = new File(ROOT_DIR + "databases/" + DATABASE_NAME);
		outfile.delete();
		if (outfile.length() <= 0) {
			AssetManager assetManager = ctx.getResources().getAssets();
			try {
				InputStream is = assetManager.open(DATABASE_NAME,
						AssetManager.ACCESS_BUFFER);
				long filesize = is.available();
				byte[] tempdata = new byte[(int) filesize];
				is.read(tempdata);
				is.close();

				outfile.createNewFile();
				FileOutputStream fo = new FileOutputStream(outfile);
				fo.write(tempdata);
				fo.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
}
